package demo
{
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.map.TiledMap;
	import laya.maths.Rectangle;
	import laya.utils.Browser;
	import laya.utils.Handler;
	
	/**
	 *@date 2017-2-17 上午10:59:04
	 *@author zhouhonghui
	 */
	public class TiledMapSample extends laya.display.Sprite
	{
		private var _tiledMap:TiledMap;
		private var _completeKey:Boolean = false;
		private var _mX:Number = 0;
		private var _mY:Number = 0;
		private var _lastMouseX:Number=0;
		private var _lastMouseY:Number=0;
		public function TiledMapSample()
		{
			super();
			createMap();
			Laya.stage.on(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.stage.on(Event.MOUSE_UP,this,onMouseUp);
			Laya.stage.on(Event.MOUSE_OUT,this,onMouseUp);
			Laya.stage.on(Event.RESIZE,this,onResize);
		}
		
		
		private function createMap():void
		{
			if(_tiledMap==null)
			{
				_tiledMap = new TiledMap();				
			}else
			{
				destroyMap();
			}
			_mX =_mY = 0;
			_tiledMap.createMap("tiledRes/desert.json",new Rectangle(0,0,Browser.width,Browser.height),Handler.create(this,completeHandler));
		}
		private function onMouseDown():void
		{
			trace("onMouseDown 111 ");
			_lastMouseX = Laya.stage.mouseX;
			_lastMouseY = Laya.stage.mouseY;
			Laya.stage.on(Event.MOUSE_MOVE,this,onMouseMove);
		}
		
		private function onMouseMove():void
		{
			trace("onMouseMove ... ");
			_tiledMap.moveViewPort(_mX-(Laya.stage.mouseX-_lastMouseX),_mY-(Laya.stage.mouseY-_lastMouseY));
		}
		
		private function onMouseUp():void
		{
			trace("onMouseUp 222 ");
			_mX = _mX-(Laya.stage.mouseX-_lastMouseX);
			_mY = _mY-(Laya.stage.mouseY-_lastMouseY);
			Laya.stage.off(Event.MOUSE_MOVE,this,onMouseMove);
		}
		
		//舞台尺寸改变
		private function onResize():void
		{
			if(_completeKey)
			{
				_tiledMap.changeViewPort(_mX,_mY,Browser.width,Browser.height);
			}
		}
		private function completeHandler():void
		{
			
		}
		private function destroyMap():void
		{
			
		}
		
	}
}